﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.GeneralClass
{
    public class User
    {
        private int userid;

        public int Userid
        {
            get { return userid; }
            set { userid = value; }
        }
        private string username;

        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        private string usertype;

        public string Usertype
        {
            get { return usertype; }
            set { usertype = value; }
        }
    }

    //对user进行操作
    public class OperateUser 
    {
        //MyDb mydb = new MyDb();
        MyDb mydb = Mdl_Class.Mydb;
        //登录查询
        public  User GetUser(string username, string password)
        {
            User userobj = null;
       
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select * from T_B_User where UserName='{0}' and PassWord='{1}'", username, password);
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        userobj = new User();
                        userobj.Userid = Convert.ToInt32(mydb.DataReader["UserId"]);
                        userobj.Username = mydb.DataReader["UserName"].ToString();
                        userobj.Password = mydb.DataReader["PassWord"].ToString();
                        userobj.Usertype = mydb.DataReader["userType"].ToString();
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetUser(string username,string password)", ex.ToString());
                }
            }
            return userobj;
        }

        //获取所有用户信息
        public  List<User> GetAllUsers()
        {
            List<User> userlist = new List<User>();

            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = "select * from T_B_User";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        User user = new User();
                        user.Userid = Convert.ToInt32(mydb.DataReader["UserId"]);
                        user.Username = mydb.DataReader["UserName"].ToString();
                        user.Password = mydb.DataReader["PassWord"].ToString();
                        user.Usertype = mydb.DataReader["userType"].ToString();
                        userlist.Add(user);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllUsers()", ex.ToString());
                }
            }
            return userlist;
        }

        //添加用户信息
        public  int AddUser(User userobj)
        {
            int i = 0;
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("insert into T_B_User(UserName,PassWord,userType) values('{0}','{1}','{2}')", userobj.Username, userobj.Password, userobj.Usertype);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddUser(UserModel userobj)", ex.ToString());
                }
            }
            return i;
        }

        //修改用户信息
        public int UpdateUser(User userobj)
        {
            int i = 0;
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("Update T_B_User set UserName='{0}',PassWord='{1}',userType='{2}' where UserId={3}", userobj.Username, userobj.Password, userobj.Usertype, userobj.Userid);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("UpdateUser(UserModel userobj)", ex.ToString());
                }
            }
            return i;
        }

        //删除用户信息
        public int DeleteUser(int userid)
        {
            int i = 0;
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("delete from T_B_User where UserId={0}", userid);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("DeleteUser(int userid)", ex.ToString());
                }
            }
            return i;
        }

        //获得相同用户名的个数
        public  int GetUserByUserName(string UserName, int UserID)
        {
            int i = 0;
            string sql = "";
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    if (UserID != 0)
                    {
                        sql = string.Format("select * from T_B_User where UserName='{0}' and UserId<>{1}", UserName, UserID);
                    }
                    else
                    {
                        sql = string.Format("select * from T_B_User where UserName='{0}'", UserName);
                    }
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        i++;
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetUserByUserName(string UserName, int UserID)", ex.ToString());
                }
            }
            return i;
        }
    }
}
